package com.wasp.collector.content;

import java.io.Serializable;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wasp.collector.data.BaseModel;
import com.wasp.collector.data.HandledDataWrapper;
import com.wasp.collector.service.akka.ActorResult;
import com.wasp.collector.service.akka.SaverActor;
import com.wasp.collector.service.akka.cluster.AkkaService;
import com.wasp.collector.service.chain.AbstractHandleChain;

import akka.actor.ActorRef;

public abstract class IContentHandler implements Serializable {
	private static final long serialVersionUID = -4578967055882237060L;
    private static final Logger log = LoggerFactory.getLogger(IContentHandler.class);

	private AbstractHandleChain handleChain;
	public IContentHandler(AbstractHandleChain handleChain){
		this.handleChain = handleChain;
	}
	public List<String> handleAndGenNext(Content c){
		if(c == null){
			log.error("content is null");
			return null;
		}
		HandledDataWrapper handledData = generateData(c);
		if(handledData == null){
			log.error("HandledDataWrapper is null,please check your generateData method");
			return null;
		}
		List<? extends BaseModel> data = handledData.getHandledData();
		List<String> nexts = handledData.getNexts();
		if(handleChain!= null){
			handleChainActor( c, data);
		}
		return nexts;
	}
	
	public abstract HandledDataWrapper generateData(Content c);
	
	//public abstract List<String> generateNext(Content c);
	
	public abstract void handleError(ActorResult errors);
	
	private void handleChainActor(Content c ,
			List<? extends BaseModel> handledData) {
		ActorRef a = AkkaService.getSystem().actorOf(
				SaverActor.props(c,handledData).withDispatcher("saver-dispatcher"), UUID.randomUUID().toString());
		// 注册actor
		a.tell(handleChain, a);
	}
	
}